//
//  OperatorService.m
//  Nurse
//
//  Created by 夏 申频 on 14-1-5.
//  Copyright (c) 2014年 Honeywell. All rights reserved.
//

#import "OperatorService.h"
#import "DataEngine.h"

NSString* const PRODUCELOGIN = @"DY_OUT_CZY";
NSString* const PRODUCECodeBQ = @"DY_Code_BQ_HS";

NSString* const NOTIFYReceiveDepartmentInfo = @"NOTIFYReceiveDepartmentInfo";
@interface OperatorService()

@property (nonatomic,strong) NSURL* url;

@end

@implementation OperatorService

-(OperatorService*)init{
    self= [super init];
    if (self) {
         self.url = [[DataEngine sharedInstance]urlForPath:@"/his/procedure"];
         self.currentOperatorWards = [NSMutableArray array];
    }
    return self;
}

+(OperatorService*) sharedInstance{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[OperatorService alloc] init];
    });
    return _sharedObject;
}

-(void)checkLoginForUsername:(NSString* )username andPassword:(NSString*)password withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName = PRODUCELOGIN;
    request.params = @{@"工号":username,@"口令":password,@"角色":@"1"};
    request.successHandle = ^(id result){
        if ([result isKindOfClass:[NSArray class]]) {
            if ([result count]>0) {
                id record = [result objectAtIndex:0];
                if (successHandler) {
                    successHandler(record);
                }
            }else{
                NSError* error = [DataEngine makeErrorForMsg:@"登陆失败:用户名或密码错误"];
                if (failHandler) {
                    failHandler(error,@"登陆错误");
                }
                
            }
        }else{
            if (failHandler) {
                NSError* error = [DataEngine makeErrorForMsg:@"从服务器获得错误的返类型，期望获得NSArray"];
                failHandler(error,@"类型错误");
            }
        }
    };
    request.failHandle = failHandler;
        [[DataEngine sharedInstance]callProcedure:request forURL:self.url];
}

-(void)queryDepartmentNameForCode:(NSString*)code withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName = PRODUCECodeBQ;
    request.params = @{@"科室":code};
    request.successHandle=^(id result){
        if ([result isKindOfClass:[NSArray class]]) {
            if ([result count]>0) {
                id record = [result objectAtIndex:0];
                if (successHandler) {
                    //NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
                    
                    successHandler(record);
                }
            }else{
                NSError* error = [DataEngine makeErrorForMsg:@"要查找的科室不存在"];
                if (failHandler) {
                    failHandler(error,@"代码错误");
                }
                
            }
        }else{
            if (failHandler) {
                NSError* error = [DataEngine makeErrorForMsg:@"从服务器获得错误的返类型，期望获得NSArray"];
                failHandler(error,@"类型错误");
            }
        }

    };
    request.failHandle = failHandler;
    [[DataEngine sharedInstance]callProcedure:request forURL:self.url];

}

@end
